﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chinaer.IDao;
using Chinaer.Common.Extensions;

namespace Chinaer.Dao.EF
{
    public class DingDanDAO : Repository<Model.Entities.Model_DingDan>, IDingDanDAO
    {
        public List<Model.Entities.Model_DingDan> GetDingDanByTime(DateTime beginTime, DateTime endTime,string dianPuID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.DingDans.Where(d => d.CreateTime>= beginTime && d.CreateTime <= endTime &&d.DianPuID.Equals(dianPuID,StringComparison.CurrentCultureIgnoreCase)).ToList();
            }
        }

        public override IList<Model.Entities.Model_DingDan> Query(string sql, int pageSize, out int recordCount, int pageIndex, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }

        public override Model.Entities.Model_DingDan GetByID(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.DingDans.FirstOrDefault(d => d.ID == id && d.IsUse == true && d.IsDelete == false);
            }
        }

        public override bool Exists(string id)
        {
            Model.Entities.Model_DingDan model_DingDan = GetByID(id);
            return model_DingDan == null ? false : true;
        }

        public override bool Add(Model.Entities.Model_DingDan entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Add(entity);
        }

        public override bool Update(Model.Entities.Model_DingDan entity)
        {
            entity.IsModelBaseNullThrowException();
            return base.Update(entity);
        }

        public override bool Delete(Model.Entities.Model_DingDan entity)
        {
            entity.IsModelBaseNullThrowException();
            entity.IsDelete = true;
            return base.Update(entity);
            //return base.Delete(entity);
        }

        public override bool Delete(string id)
        {
            Model.Entities.Model_DingDan model_DingDan = GetByID(id);
            return base.Delete(model_DingDan);
        }

        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                using (System.Transactions.TransactionScope scope = Chinaer.Core.Helper.TransactionHelper.Instance.BeginTransaction())
                {
                    var queryResult = from p in context.DingDans
                                      where listID.Contains(p.ID) && p.IsUse==true&&p.IsDelete==false
                                      select p;
                    foreach (var item in queryResult)
                    {
                        context.Entry<Model.Entities.Model_DingDan>(item).State = System.Data.Entity.EntityState.Modified;
                        item.IsDelete = true;
                    }
                    int result = context.SaveChanges();
                    scope.Complete();
                    return result;
                }
            }
        }
    }
}
